/**
 * 
 */
package com.iwords.service.ao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import com.iwords.enums.FeedbackStatus;
import com.iwords.service.UserService;
import com.iwords.service.UserServiceLocal;
import com.iwords.service.object.FeedbackEntity;
import com.iwords.service.object.UserEntity;
import com.iwords.service.object.UserSysConfigEntity;
import com.iwords.service.persistence.FeedbackPersistence;
import com.iwords.service.persistence.UserPersistence;
import com.iwords.utils.DateUtils;

/**
 * @author       BOLIN.HBC 
 * @time         2011-3-6    
 * @description  USER-SERVICEIMPL       
 */
@Stateless
public class UserServiceImpl implements UserServiceLocal,UserService {
	
	@EJB
	UserPersistence userPersistence;
	
	@EJB
	FeedbackPersistence feedbackPersistence;

	public void create(UserEntity entity) {
		userPersistence.create(entity);
	}

	public void edit(UserEntity entity) {
		UserEntity user = userPersistence.find(entity.getUserid());

		if (entity.getBirthday() != null)
			user.setBirthday(entity.getBirthday());
		if (entity.getNickname() != null)
			user.setNickname(entity.getNickname());
		if (entity.getPassword() != null)
			user.setPassword(entity.getPassword());

		if (entity.getQq() != null)
			user.setQq(entity.getQq());
		if (entity.getGender() != null)
			user.setGender(entity.getGender());
		if (entity.getPhone() != null)
			user.setPhone(entity.getPhone());
		user.setUpdatetime(new Date());
		userPersistence.edit(user);
	}

	public void remove(UserEntity entity) {
		userPersistence.remove(entity);
	}

	public UserEntity find(Object id) {
		return userPersistence.find(id);
	}


	public List<UserEntity> findAll() {
		return userPersistence.findAll();
	}

	public List<UserEntity> findRange(int[] range) {
		return userPersistence.findRange(range);
	}

	public int count() {
		return userPersistence.count();
	}

	public UserEntity findUser(UserEntity user) {
 		return userPersistence.find(user);
	}

	 
	public boolean isContain(UserEntity user) {
		 
		return userPersistence.isContain(user);
	}

	@Override
	public void updateCurrentLib(UserEntity user) {
		userPersistence.updateCurrentLib(user);
		
	}

	@Override
	public boolean updateUserSysConfig(UserEntity userEntity) {
		UserEntity user = userPersistence.find(userEntity.getUserid());
		if(user == null){
			return false;
		}else{
			try {
				List<UserSysConfigEntity> existSysConfigs = user.getUserSysConfigs();
				List<UserSysConfigEntity> newSysConfigs = userEntity.getUserSysConfigs();
				
				Date operTime =new Date();
				Map<String,UserSysConfigEntity> sysConfigsMap = new HashMap<String, UserSysConfigEntity>();
				
				for(UserSysConfigEntity userSysConfig : existSysConfigs){
					if(sysConfigsMap.get(userSysConfig.getConfigkey())== null){
						sysConfigsMap.put(userSysConfig.getConfigkey(), userSysConfig);
					}
				}
				
				for(UserSysConfigEntity newUserSysConfig : newSysConfigs){
					UserSysConfigEntity sysConfig = sysConfigsMap.get(newUserSysConfig.getConfigkey());
					if(sysConfig != null){
						sysConfig.setConfigvalue(newUserSysConfig.getConfigvalue());
						sysConfig.setUpdatetime(operTime);
					}else{
						newUserSysConfig.setUser(user);
						newUserSysConfig.setCreatetime(operTime);
						newUserSysConfig.setUpdatetime(operTime);
						sysConfigsMap.put(newUserSysConfig.getConfigkey(), newUserSysConfig);
						//添加到关系
						user.addSysConfig(newUserSysConfig);
					}
				}
				
				userPersistence.edit(user);
				return true;
			} catch (Exception e) {
				//TODO add log
				return false;
			}
			
			
		}
	}
	
	@Override
	public boolean updateUserVacation(UserEntity userEntity,int delayDays) {
		UserEntity user = userPersistence.find(userEntity.getUserid());
		if(user == null){
			return false;
		}else{
			user.getUserVacation().setExpireDate(DateUtils.getDateBeforeOrAfter(user.getUserVacation().getExpireDate(),delayDays));
			user.getUserVacation().setUpdatetime(new Date());
			userPersistence.edit(user);
			return true;
		}
		
	}
        
        public boolean updateUserVacationFormNow(UserEntity userEntity,int delayDays){
            UserEntity user = userPersistence.find(userEntity.getUserid());
		if(user == null){
			return false;
		}else{
			user.getUserVacation().setExpireDate(DateUtils.getFormatDateTime(DateUtils.getFormatDateTime(DateUtils.getDateBeforeOrAfter(new Date(), delayDays), "yyyyMMdd")+ " 23:59:59", "yyyyMMdd hh:mm:ss"));
			user.getUserVacation().setUpdatetime(new Date());
			userPersistence.edit(user);
			return true;
		}
        }

	@Override
	public List<FeedbackEntity> findLatestFeedbacks() {
		
		return feedbackPersistence.findLatestFeedbacks();
	}

	@Override
	public boolean addFeedback(Long userid,FeedbackEntity feedback) {
		UserEntity user = userPersistence.find(userid);
		if(user == null){
			return false;
		}
		Date operTime = new Date();
		feedback.setUser(user);
		feedback.setCreatetime(operTime);
		feedback.setUpdatetime(operTime);
		feedback.setStatus(FeedbackStatus.UNHANDLE.getStatus());
		feedbackPersistence.create(feedback);
		
		return true;
	}

	public UserSysConfigEntity getUserConfigByUserIdToday(UserSysConfigEntity config) {
		return null;
	}
	
	
}
